﻿using Com.JunCaiSoft.API.Common.AssistClass;
using Com.JunCaiSoft.API.WXWork.Dto;
using Com.JunCaiSoft.AutoMapper;
using Com.JunCaiSoft.Common.Controller;
using Com.JunCaiSoft.Common.Enums;
using Com.JunCaiSoft.Common.Extension;
using Com.JunCaiSoft.Common.Model;
using Com.JunCaiSoft.Hr.Entity;
using Com.JunCaiSoft.Hr.IDao;
using Com.JunCaiSoft.Hr.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Com.JunCaiSoft.API.Hr.Controllers
{
    /// <summary>
    /// 部门管理模块
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class PartmentController : BaseController<PartmentView, PartmentItem, Partment, Guid>
    {
        public PartmentController(IPartmentDao partmentdao) : base(partmentdao)
        {
            //var deptlist = RedisCacheExtension.GetStringValue("PartmentList");

            //if (String.IsNullOrEmpty(deptlist) || deptlist == "null" || deptlist == "[]")
            //{
            //    var token = Request.Headers["Authorization"];

            //    HttpExtension.HttpGet("",true,"",token);
            //}

            //var deptList = JsonConvert.DeserializeObject<List<Department>>(deptlist);
        }
        /// <summary>
        /// 按部门ID单个删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete]
        public override async Task<ActionResult> DeleteSingleAsync(Guid Id)
        {
            try
            {
                await DaoAsync.dbContext.Database.BeginTransactionAsync();
                int result = await DaoAsync.DeleteListAsync<Partment>(x => x.Id == Id);

                return await Task.Run(() => Content(JsonConvert.SerializeObject(result)));
            }
            catch (Exception e)
            {
                await DaoAsync.dbContext.Database.RollbackTransactionAsync();
                throw e;
            }
            finally
            {
                DaoAsync.dbContext.Database.CommitTransaction();
            }
        }
        /// <summary>
        /// 按部门ID批量删除
        /// </summary>
        /// <param name="DeleteList"></param>
        /// <returns></returns>
        [HttpPatch]
        public virtual async Task<ActionResult> DeleteList(IEnumerable<Guid> DeleteList)
        {
            try
            {
                await DaoAsync.dbContext.Database.BeginTransactionAsync();

                int result = await DaoAsync.DeleteListAsync<Partment>(x => DeleteList.Contains(x.Id));

                return await Task.Run(() => Content(JsonConvert.SerializeObject(result)));
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                DaoAsync.dbContext.Database.CommitTransaction();
            }
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize"></param>
        /// <param name="keyword">关键字</param>
        /// <returns></returns>
        [HttpGet]
        public virtual async Task<ActionResult> ListByPageAsync(int pageIndex, int pageSize, String keyword)
        {
            try
            {
                // ,

                if (String.IsNullOrEmpty(keyword))
                {
                    keyword = "";
                }

                var resultList = await DaoAsync.ListByPageAsync(x => x.Name.Contains(keyword) || x.NickName.Contains(keyword), x => x.CreateTime, pageIndex, pageSize,OrderbyType.desc);

                return await Task.Run(() => Content(JsonConvert.SerializeObject(resultList)));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// 创建部门
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public override async Task<ActionResult> CreateAsync(PartmentView entity)
        {
            try
            {
                DaoAsync.dbContext.Database.BeginTransaction();

                var saveone = entity.MapTo<Partment>();
                var result = await DaoAsync.InsertAsync(saveone);

                DaoAsync.dbContext.Database.CommitTransaction();
                return await Task.Run(() => Content(JsonConvert.SerializeObject(result)));
            }
            catch (Exception e)
            {
                throw e;
            }

        }
        [HttpPut]
        public override async Task<ActionResult> UpdateAsync(UpdateEntity<PartmentView> entityModel)
        {
            DaoAsync.dbContext.Database.BeginTransaction();

            var saveone = entityModel.entity.MapTo<Partment>();
            var result = await DaoAsync.UpdateAsync(saveone, entityModel.Id);

            DaoAsync.dbContext.Database.CommitTransaction();
            return await Task.Run(() => Content(JsonConvert.SerializeObject(result)));
        }
        [HttpGet]
        public override async Task<ActionResult> EntityList(String keyword)
        {
            try
            {
                List<PartmentItem> returnList = new List<PartmentItem>();

                var entitylist = new List<Partment>();

                if (String.IsNullOrEmpty(keyword))
                {
                    entitylist = await DaoAsync.ListAsync();
                }
                else
                {
                    entitylist = (await DaoAsync.ListAsync(x => x.Name.EndsWith(keyword) && x.Name != keyword));
                }

                entitylist.ForEach(item =>
                {
                    returnList.Add(item.MapTo<PartmentItem>());
                });

                return await Task.Run(() => Content(JsonConvert.SerializeObject(returnList)));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// 通过公司Code查询部门
        /// </summary>
        /// <param name="PartmentCode"></param>
        /// <returns></returns>
        [HttpGet]
        public virtual async Task<ActionResult> QueryByCode(String PartmentCode)
        {
            try
            {
                var list = DaoAsync.GetAsync(x => x.NumCode == PartmentCode).Result;
                return await Task.Run(() => Content(JsonConvert.SerializeObject(list)));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
